package ks.client.lobby;


import java.awt.Component;
import java.awt.GridBagLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.GridBagConstraints;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;

import ks.client.UserContext;
import ks.client.controllers.ProcessLobbyInputController;
import ks.client.controllers.commands.IconListCommandController;
import ks.client.controllers.commands.PluginListCommandController;
import ks.client.controllers.framework.ConnectController;
import ks.client.controllers.framework.DisconnectController;
import ks.client.gui.GMGUI;
import ks.client.gui.GMTOPT;
import ks.client.gui.TMGUI;
import ks.client.gui.UMGUI3;
import ks.client.gui.UMGUI3CHAT;
import ks.client.gui.admin.AdminControlGUI;
import ks.client.gui.admin.plugin.PluginAdminGUI;
import ks.client.gui.plugin.PMGUI;
import ks.client.gui.user.AccountUpdateGUI;
import ks.client.gui.user.listeners.UpdateAccountClickController;
import ks.client.gui.user.listeners.getUserIconPic;
import ks.managers.user.User;

public class RoomGUI extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel topPanel = null;
	private JLabel userL = null;
	private JTextField userField = null;
	private JLabel passwordLabel = null;
	private JPasswordField passwordField = null;
	private JTextField lobbyInput = null;
	private UMGUI3 userManagerGUI = null;
	private TMGUI  tableManagerGUI = null;
	private PMGUI  pluginManagerGUI = null;
	private JLabel jLabel1 = null;
	private JScrollPane tblScrollPanel = null;
	private JScrollPane userScrollPanel = null;
	private JScrollPane pluginScrollPanel = null;
	private JScrollPane lobbyScrollbarPanel = null;
	private JTextArea lobbyOutput = null;
	private JCheckBox selfRegisterCheck = null;
	private JButton connectButton = null;
	private JButton updateButton = null;
	private JPanel innerPanel = null;
	private GMGUI gameManagerGUI = null;
	private GMTOPT gameManagerOptionsGUI = null;
	private UMGUI3CHAT userManagerGUIchat = null;
	private PluginAdminGUI pluginAdminGUI = null;
	private AccountUpdateGUI accountUpdateGUI = null;
	private AdminControlGUI adminControlGUI = null;
	
	/** Lobby in which we are enclosed. */
	Lobby lobby;
	
	/** Context. */
	UserContext context;
	
	/**
	 * This is the default constructor
	 */
	public RoomGUI(Lobby lobby) {
		super();
		
		this.lobby = lobby;
		this.context = lobby.getUserContext();
		initialize();
	}


	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		
		this.setLayout(new FlowLayout());
		
		this.add(getTopPanel());
		
		// Panel to be added.
		innerPanel = new JPanel();
		innerPanel.setPreferredSize(new Dimension(800, 750));
		innerPanel.setLayout(new GridLayout(2,2));
		
		innerPanel.add(getTblScrollPanel());
		innerPanel.add(getUserScrollPanel());
		
		JPanel triPanel = new JPanel();
		triPanel.setPreferredSize(new Dimension (400, 200));
		triPanel.setLayout(new FlowLayout());
		triPanel.add(getLobbyScrollbarPanel());
		triPanel.add(getLobbyInput());
		
		innerPanel.add(triPanel);
		innerPanel.add (getPluginScrollPanel());
		
		this.add(innerPanel);
		
		this.invalidate();
		this.revalidate();
	}

	/**
	 * This method initializes topPanel	
	 * 	
	 * @return java.awt.Panel	
	 */
	JPanel getTopPanel() {
		if (topPanel == null) {
			/*passwordLabel = new JLabel();
			passwordLabel.setText("Password:");
			passwordLabel.setName("passwordLabel");
			
			userL = new JLabel();
			userL.setText("User:");
			userL.setName("userLabel");*/
			
			topPanel = new JPanel();
			topPanel.setLayout(new FlowLayout());
			topPanel.setBackground(SystemColor.control);
			
			topPanel.setSize(820, 37);
			
			//topPanel.add(userL);
			//topPanel.add(getUserField());
			//topPanel.add(passwordLabel);
			//topPanel.add(getPasswordField());
			//topPanel.add(getSelfRegisterCheck());
			topPanel.add(getConnectButton());
			topPanel.add(getUpdateButton());
			
		}
		return topPanel;
	}

	
	/**
	 * This method initializes userField	
	 * 	
	 * @return java.awt.TextField	
	 */
	JTextField getUserField() {
		if (userField == null) {
			userField = new JTextField();
			userField.setName("userField");
			userField.setPreferredSize(new Dimension(180, 23));
			userField.setText("UserName");
		}
		return userField;
	}

	/**
	 * This method initializes textField	
	 * 	
	 * @return java.awt.TextField	
	 */
	JPasswordField getPasswordField() {
		if (passwordField == null) {
			passwordField = new JPasswordField();
			passwordField.setPreferredSize(new Dimension(180, 23));
			passwordField.setText("*******");
			passwordField.setName("passwordField");
		}
		return passwordField;
	}

	/**
	 * this method initializes pchat
	 */
	public UMGUI3CHAT getUserManagerGUIchat(){
		if(userManagerGUIchat == null){
			userManagerGUIchat = new UMGUI3CHAT(lobby);
		}
		return userManagerGUIchat;
	}
	/**
	 * This method initializes userManagerGUI	
	 * 	
	 */
	public UMGUI3 getUserManagerGUI() {
		if (userManagerGUI == null) {
			userManagerGUI = new UMGUI3(lobby);
		}
		return userManagerGUI;
	}
	
	public boolean newGameManagerGUI(int tableNum) {
		gameManagerGUI = new GMGUI(lobby, tableNum);
		return true;
	}

	/** 
	 * This method initializes TableManagerGUI
	 */
	public TMGUI getTableManagerGUI() {
		if (tableManagerGUI == null) {
			tableManagerGUI = new TMGUI(lobby);
		}
		return tableManagerGUI;
	}

	/**
	 * This method initializes pluginManagerGUI	
	 * 	
	 */
	public PMGUI getPluginManagerGUI() {
		if (pluginManagerGUI == null) {
			pluginManagerGUI = new PMGUI(lobby);
		}
		return pluginManagerGUI;
	}
	/**
	 * this method initializes gameManagerGUI
	 * 
	 */
	public GMGUI getGameManagerGUI(int num){
		if(gameManagerGUI == null){
			this.newGameManagerGUI(num);
		}
		return gameManagerGUI;
	}
	public boolean newGameManagerOptionsGUI(int tableNum) {
		gameManagerOptionsGUI = new GMTOPT(lobby, tableNum);
		return true;
	}
	
	public GMTOPT getGameManagerOptionsGUI(int num) {
		if(gameManagerOptionsGUI == null){
			this.newGameManagerOptionsGUI(num);
		
	}
		return gameManagerOptionsGUI;
	}
	
	public GMGUI getGameManagerGUI(){
		return gameManagerGUI;
	}
	
	
	public PluginAdminGUI getPluginAdminGUI(){
		if(pluginAdminGUI == null){
			pluginAdminGUI = new PluginAdminGUI(lobby);
		}
		return pluginAdminGUI;
	}
	
	public AccountUpdateGUI getAccountUpdateGUI(){
		if(accountUpdateGUI == null){
			accountUpdateGUI = new AccountUpdateGUI(lobby);
			new getUserIconPic(lobby).setIcon();

		}
		return accountUpdateGUI;
	}
	
	public AdminControlGUI getAdminControlGUI(){
		if(adminControlGUI == null){
			adminControlGUI = new AdminControlGUI(lobby);
			}
		return adminControlGUI;
	}
	
	
	/**
	 * This method initializes tblScrollPanel	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getTblScrollPanel() {
		if (tblScrollPanel == null) {
			tblScrollPanel = new JScrollPane();
			//tblScrollPanel.setBounds(new Rectangle(11, 74, 581, 333));
			tblScrollPanel.setPreferredSize(new Dimension(100, 333));
			tblScrollPanel.setViewportView(getTableManagerGUI());
			tblScrollPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
			tblScrollPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		}
		return tblScrollPanel;
	}

	/**
	 * This method initializes userScrollPanel	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getUserScrollPanel() {
		if (userScrollPanel == null) {
			userScrollPanel = new JScrollPane();
			//userScrollPanel.setBounds(new Rectangle(603, 73, 239, 635));
			userScrollPanel.setPreferredSize(new Dimension(100, 333));
			userScrollPanel.setViewportView(getUserManagerGUI());
			userScrollPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
			userScrollPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

		}
		return userScrollPanel;
	}
	
	/**
	 * This method initializes pluginScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getPluginScrollPanel() {
		if (pluginScrollPanel == null) {
			pluginScrollPanel = new JScrollPane();
			//userScrollPanel.setBounds(new Rectangle(603, 73, 239, 635));
			pluginScrollPanel.setPreferredSize(new Dimension(100, 80));
			pluginScrollPanel.setViewportView(getPluginManagerGUI());
			pluginScrollPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
			pluginScrollPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

		}
		return pluginScrollPanel;
	}

	/**
	 * This method initializes lobbyScrollbarPanel	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getLobbyScrollbarPanel() {
		if (lobbyScrollbarPanel == null) {
			lobbyScrollbarPanel = new JScrollPane();
			lobbyScrollbarPanel.setPreferredSize(new Dimension(400, 226));
			lobbyScrollbarPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
			lobbyScrollbarPanel.setViewportView(getLobbyOutput());
			lobbyScrollbarPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

		}
		return lobbyScrollbarPanel;
	}

	/**
	 * This method initializes lobbyOutput	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	public JTextArea getLobbyOutput() {
		if (lobbyOutput == null) {
			lobbyOutput = new JTextArea();
			lobbyOutput.setRows(8);
		}
		return lobbyOutput;
	}
	
	/**
	 * This method initializes lobbyOutput	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	JTextField getLobbyInput() {
		if (lobbyInput == null) {
			lobbyInput = new JTextField(36);
			//lobbyInput.setBounds(11, 650, 581, 24);
			lobbyInput.setPreferredSize(new Dimension(50, 24));
			
			lobbyInput.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					String s = lobbyInput.getText();
					lobbyInput.setText("");

					// hand off to the appropriate controller.
					new ProcessLobbyInputController(context).process(s);
				}
				
			});
		}
		return lobbyInput;
	}

	/**
	 * Append text to the lobby.
	 * 
	 * @param s
	 */
	public void append (String s) {
		String t = lobbyOutput.getText() + "\n" + s;
		lobbyOutput.setText(t);
		scrollToEnd();		
	}
	
	/**
	 * Make sure we can scroll to the end on demand.
	 */
	void scrollToEnd() {
		lobbyOutput.setCaretPosition(lobbyOutput.getDocument().getLength());
	}
	
	/**
	 * Self-register checkbox	
	 * 	
	 * @return javax.swing.JButton	
	 */
	JCheckBox getSelfRegisterCheck() {
		if (selfRegisterCheck == null) {
			selfRegisterCheck = new JCheckBox();
			selfRegisterCheck.setText("Create Account");
		}
		
		return selfRegisterCheck;
	}
	
	/**
	 * This method initializes updateButton
	 * 
	 * @return javax.swing.JButton
	 */
	JButton getUpdateButton(){
		if(updateButton == null){
			updateButton = new JButton();
			updateButton.addActionListener(new UpdateAccountClickController(lobby));
			updateButton.setText("Update Account");
		}
		return updateButton;
	}
	
	
	/**
	 * This method initializes connectButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	JButton getConnectButton() {
		if (connectButton == null) {
			connectButton = new JButton();
			connectButton.setText("Disconnect");
			
			connectButton.addActionListener(new ActionListener() {

				/** Attempt to connect via our connection agent. */
				public void actionPerformed(ActionEvent e) {
					System.out.println("DISCONNECT BUTTON HAS BEEN TRIGGERED");
					getUserManagerGUI().removeUser(context.getUser());
					if (new DisconnectController(lobby).process(context)) {
						connectButton.setText("Connect");
						System.out.println("USER: " + context.getUser() + " HAS BEEN REMOVED FROM USERTABLE");
					}
				}
			});
		}
		return connectButton;
	}

	/**
	 * Enable the clean reset of login credentials to original user context.
	 * 
	 */
	public void resetLogin() {
		getUserField().setText(context.getUser());
		getPasswordField().setText(context.getPassword());
	}




	

}  //  @jve:decl-index=0:visual-constraint="10,10"
